<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Guests_model extends CI_Model {
  
  public function validate_password($email, $password)
  {
    if ($this->user_exists($email))
    {
      $password = $this->encrypt_password($password, $this->get_salt($email));
      
      $this->db
        ->from('guests')
        ->where('email', $email)
        ->where('password', $password);
      
      $query = $this->db
        ->get()
        ->row_object();
      
      return !empty($query);
    }
    else
      return FALSE;
  }
  
  // ===========================================================================
  
  public function get_user_data($select, $email)
  {
    $this->db
      ->select($select)
      ->from('guests')
      ->where('email', $email);
    
    return $this->db
        ->get()
        ->row_object()
        ->$select;
  }
  
  // ===========================================================================
  
  public function get_user_group($group_id, $email)
  {
    $name = $this->get_user_data('name', $email);
    
    $this->db
      ->select('name')
      ->from('guests')
      ->where('group_id', $group_id)
      ->not_like('email', $email);
    
    $guests = $this->db
      ->get()
      ->result_object();
    
    $group = '';
    
    for ($i = 0; $i < count($guests); $i++)
      $group[$i] = $guests[$i]->name;
    
    return $group;
  }
  
  // ===========================================================================
  
  public function register($data)
  {
    $data['reset_code'] = $this->generate_random_string(10);
    
    $this->db->insert('guests', $data);
    
    return $data['reset_code'];
  }
  
  // ===========================================================================
  
  public function get_new_group()
  {
    $this->db
      ->select_max('group_id')
      ->from('guests');
    
    return $this->db
      ->get()
      ->row_object()
      ->group_id + 1;
  }
  
  // ===========================================================================
  
  public function update_user($data, $email)
  { 
    $this->db
      ->where('email', $email)
      ->update('guests', $data);
  }
  
  // ===========================================================================
  
  public function update_password($password, $email)
  {
    $data['salt']     = $this->generate_random_string();
    $data['password'] = $this->encrypt_password($password, $data['salt']);
    
    $this->db
      ->where('email', $email)
      ->update('guests', $data);
  }
  
  // ===========================================================================
  
  public function validate_email($email)
  {
    return !$this->user_exists($email);
  }
  
  // ===========================================================================
  
  public function set_reset_code($email)
  {
    $data['reset_code'] = $this->generate_random_string(10);
    
    $this->db
      ->where('email', $email)
      ->update('guests', $data);
  }
  
  // ===========================================================================
  
  public function get_reset_email($reset_code)
  {
    $this->db
      ->select('email')
      ->from('guests')
      ->where('reset_code', $reset_code);
    
    $query = $this->db
      ->get()
      ->row_object();
    
    if (!empty($query))
      return $query->email;
    else
      return '';
  }
  
  // ===========================================================================
  
  public function get_loaned_items($guest_id)
  {
    $this->db
      ->from('loan')
      ->where('guest_id', $guest_id);
    
    return $this->db
      ->get()
      ->result_array();
  }
  
  // ===========================================================================
  
  private function generate_random_string($length = 50)
  {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $string     = '';
    
    for ($i = 0; $i < $length; $i++)
      $string .= $characters[rand(0, strlen($characters) - 1)];
    
    return $string;
  }
  
  // ===========================================================================
  
  private function get_salt($email)
  {
    $this->db
      ->select('salt')
      ->from('guests')
      ->where('email', $email);
    
    return $this->db
      ->get()
      ->row_object()
      ->salt;
  }
  
  // ===========================================================================
  
  private function user_exists($email)
  {
    $this->db
      ->from('guests')
      ->where('email', $email);
    
    $query = $this->db
      ->get()
      ->row_object();
    
    return !empty($query);
  }
  
  // ===========================================================================
  
  private function encrypt_password($password, $salt)
  {
    $encrypt = sha1($password) . sha1($salt);
    
    for ($i = 0; $i < 3; $i++)
      $encrypt = sha1($encrypt);
    
    return $encrypt;
  } 
}